package com.googlecode.olabs.clients.fileSync;

import java.awt.Menu;
import java.awt.MenuBar;
import java.io.IOException;
import java.util.Arrays;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JSeparator;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import com.googlecode.olabs.clients.fileSync.contacts.ContactLibrary;
import com.googlecode.olabs.clients.fileSync.contacts.ShellListWrapper;
import com.googlecode.olabs.common.client.contact.Contact;
import com.googlecode.olabs.common.client.contact.ContactException;
import com.googlecode.olabs.common.client.contact.ContactImpl;
import com.googlecode.olabs.common.client.contact.field.Field;
import com.googlecode.olabs.common.client.contact.field.FormattedNameField;
import com.googlecode.olabs.common.client.contact.field.IdField;
import com.googlecode.olabs.common.desktop.JFrameClose;

public class FileSync {

	private JFrameClose m_mainWindow;

	public FileSync(final ShellListWrapper listWrapper) {

		// Create contact list
		final JList contactList = new JList(listWrapper);
		contactList
				.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
		contactList.setLayoutOrientation(JList.VERTICAL);
		contactList.setVisibleRowCount(-1);

		// Create content pane
		final JLabel currentContact = new JLabel();
		currentContact.setText("This will display a simple contact");

		// Create buttons
		final Box buttonArea = new Box(BoxLayout.X_AXIS);
		buttonArea.add(new JButton("Button 1"));
		buttonArea.add(new JButton("Button 2"));
		buttonArea.add(new JButton("Button 3"));

		// Compile layout
		final Box outerBox = new Box(BoxLayout.Y_AXIS);
		final Box upperBox = new Box(BoxLayout.X_AXIS);
		outerBox.add(upperBox);
		outerBox.add(new JSeparator());
		outerBox.add(buttonArea);
		upperBox.add(contactList);
		upperBox.add(new JSeparator(SwingConstants.VERTICAL));
		upperBox.add(currentContact);

		m_mainWindow = new JFrameClose("OLABS - File Sync");
		m_mainWindow.getContentPane().add(outerBox);
		m_mainWindow.setMenuBar(createMenu());
		m_mainWindow.setSize(320, 174);
		m_mainWindow.setLocation(300, 200);
		m_mainWindow.setResizable(true);

		// Wire listeners
		ListSelectionListener listListener = new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent e) {
				currentContact.setText(listWrapper.getContactAt(
						contactList.getSelectedIndex()).toString());
			}
		};
		contactList.addListSelectionListener(listListener);
	}

	public void show() {
		m_mainWindow.setVisible(true);
	}

	private MenuBar createMenu() {
		MenuBar menuBar = new MenuBar();
		menuBar.add(new Menu("About (Broken)"));
		return menuBar;
	}

	/**
	 * Main function
	 */
	public static void main(String[] args) throws SecurityException,
			IOException, ContactException {
		ContactLibrary contactLibrary = new ContactLibrary();
		FileSync mainWindow = new FileSync(ShellListWrapper
				.wrap(contactLibrary));
		mainWindow.show();

		Contact contact1 = new ContactImpl();
		Field field = new IdField();
		field.setValue(Arrays.asList(new String[] { "id1" }));
		contact1.getFields().add(field);
		field = new FormattedNameField();
		field.setValue(Arrays.asList(new String[] { "Ebenzer Tweed" }));
		contact1.getFields().add(field);

		contactLibrary.addContact(contact1);
	}
}